var paymentOrderType = getStatusData("/global/paymentOrderType.html");
var currencysData = getStatusData("/global/currencys.html");
var payMethodArray = getStatusData("/global/payMethod.html");
var paidStatus = getStatusData("/global/paidStatus.html");
var payMethodJson = [];
$.each(payMethodArray, function (index, value) {
    var payMethodRow = {}
    payMethodRow.id = index;
    payMethodRow.orName = value;
    payMethodJson.push(payMethodRow);
});

//获取付款申请列表的list
//付款申请单号、付款申请状态、供应商名称、申请金额、实付金额、货币代码、申请人、支付人、付款号、付款日期、支付方式、申请日期、备注
function getPayOptions(options) {
    var initOptions = {
        idField: 'paidAppNumber',
        columns: [[{
            checkbox: true
        }, {
            title: '付款申请号',
            field: 'paidAppNumber',
            width: 120
        }, {
            field: 'paidStatus',
            title: '付款申请状态',
            width: 100
        }, {
            field: 'providerName',
            title: '供应商名称',
            width: 120
        }, {
            field: 'appAmount',
            title: '申请金额',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'realAmount',
            title: '实付金额',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'currencyCode',
            title: '货币代码',
            width: 80
        }, {
            field: 'operater',
            title: '申请人',
            width: 100
        }, {
            field: 'payPerson',
            title: '支付人',
            width: 100
        }, {
            field: 'paidNumber',
            title: '付款号',//TODO 付款单号
            width: 120
        }, {
            field: 'payDate',
            title: '付款日期',
            width: 100
        }, {
            field: 'payMethod',
            title: '支付方式',
            width: 100
        }, {
            field: 'paidStarttime',
            title: '申请日期',
            width: 100
        }, {
            field: 'remark',
            title: '备注',
            width: 300
        }]],
        toolbar: [{
            id:"payReal",
            disabled:true,
            iconCls: 'icon-edit',
            text:"财务付款",
            handler: function(){addPayFunction()}
        },'-',{
            id:"payDetail",
            disabled:true,
            iconCls: 'icon-tip',
            text:"明细",
            handler: function(){editPayDetailFunction()}
        }],
        onCheck:function(rowIndex,rowData){
            if(rowData.paidStatus ==="审批通过"){
                $('#payReal').linkbutton('enable');
            }else{
                $('#payReal').linkbutton('disable');
            }
            if(rowData.paidStatus ==="财务拨付"){
                $('#payDetail').linkbutton('enable')
            }else{
                $('#payDetail').linkbutton('disable')
            }
            var postData = {'id':rowData.id};
            postAjax("/payment/selectPaidApplicationSub.html",postData,
                refreshPaySubArea, {type:'get',async: false});
        }
    }
    if (options != undefined) {
        $.extend(initOptions, options);
    }
    return initOptions;

}


//获取付款申请列表的list
//付款申请号、付款申请子项号、付款对象编号、采购合同号、含税货款金额、总金额、申请金额、实付金额、供应商合同号、款项类型、付款基准日、备注。
function getPaySubDetailOptions(flag) {
    var initOptions = {
        idField: 'cid',
        fitColumns: false,
        checkOnSelect: false,
        singleSelect: false,
        fit: true,
        height:200,
        columns: [[
            {
            field: 'paidAppNum',
            title: '付款申请号',
            width: 100
        }, {
            field: 'padAppSub',
            title: '付款申请子项号',
            width: 100
        }, {
            field: 'contractCode',
            title: '付款对象编号',//TODO
            width: 100
        }, {
            title: '采购合同号',
            field: 'contractCode',
            width: 100
        }, {
            title: '含税货款金额',
            field: 'taxAmount',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'totalAmount',
            title: '总金额',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }

        }, {
            field: 'appBalance',
            title: '申请金额',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'paidAmount',
            title: '实付金额',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        },
            {
                field: 'privoderCcode',
                title: '供应商合同号',
                width: 110
            }, {
                field: 'moneyType',
                title: '款项类型',
                width: 100,
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    for (var i = 0; i < payMethodJson.length; i++) {
                        if (payMethodJson[i].id == value) {
                            return payMethodJson[i].orName;
                        }
                    }
                }
            }, {
                field: 'paymentDate',
                title: '付款基准日',
                width: 100,
                align:'right'
            }, {
                field: 'remark',
                title: '备注',
                width: 200
            }]]
    }
    return initOptions;
}

function refreshPaySubArea(data){
    var options = {};
    options.data = data.object.paidApplicationSubList;
    initDataGridNoPager($('#payApDetailDataGrid'), options);
}

//
function calculatePayTop() {
    var subRows = $('#subItem').datagrid('getRows');
    var payAmount = 0;
    $.each(subRows, function (index, value) {
        payAmount = payAmount.add(parseFloat(value.payAmount ? value.payAmount : 0));
    });
    $('#realAmount').val(payAmount.toFixed(2));
}


function getPayDetail(data){
    parent.$.modalDialog.handler.dialog('close');
    var options = {"id": data.object.paymentOrder.f_paidId};
    editPayDetailFunction(options);
}

function refreshPayList(data){
    parent.$.modalDialog.handler.dialog('close');
    //if(parent.$.modalDialog&&parent.$.modalDialog.openner_treeGrid){
    //    parent.$.modalDialog.openner_treeGrid.datagrid('getPager').pagination('select');
    //}
    $.messager.alert("Info", "红冲成功", 'info');
}
